package com.jmx.yp.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 * 
 * 功能描述：
 *        生成验证码
 * @author Jenny
 *
 * Date:2011-1-9 下午09:43:04
 */
public class VerifyCodeEncoder {
	public VerifyCodeEncoder() {
	}

	public void initialize() {
	}

	public void cleanup() {
	}

	/**
	 * 
	 * 将传入的字符串编码成图片方式，然后写入输出流
	 * 
	 */
	public static void encode(OutputStream outputstream, String s)
			throws IOException {

		if (s == null || s.length() == 0) {
			throw new NullPointerException(
					"Param verifycode cannot be null or empty.");
		}
		if (outputstream == null) {
			throw new NullPointerException(
					"Param Outputstream out cannot be null.");
		}

		char[] ac = s.toCharArray();
		Font[] afont = new Font[ac.length];
		int[] ai = new int[ac.length];
		int i = 0;
		byte byte0 = 20;
		byte byte1 = 14;
		int j = FONTS.length;
		for (int k = 0; k < afont.length; k++) {
			int l = random.nextInt(j);
			afont[k] = FONTS[l];
			int k1 = (int) ((double) FONTMETRICS[l].charWidth(ac[k]) * 1.0D);
			ai[k] = k1;
			ai[k] = k1 >= byte1 ? k1 : ((int) (byte1));
			i += ai[k];
		}

		i = (int) ((double) i * 1.1000000000000001D);
		BufferedImage bufferedimage = new BufferedImage(i, byte0, 1);
		Graphics g = bufferedimage.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, i, byte0);
		g.setColor(Color.lightGray);
		for (int i1 = 0; i1 < 155; i1++) {
			int l1 = random.nextInt(i);
			int j2 = random.nextInt(byte0);
			int k2 = random.nextInt(50);
			int l2 = random.nextInt(5);
			g.drawLine(l1, j2, l1 + k2, j2 + l2);
		}

		int j1 = 0;
		for (int i2 = 0; i2 < ac.length; i2++) {
			char c = ac[i2];
			g.setColor(Color.BLACK);
			g.setFont(afont[i2]);
			j1 += i2 != 0 ? ai[i2] : 2;
			g.drawString(String.valueOf(c), j1, 16);
		}
		g.dispose();
		ImageIO.write(bufferedimage, EXTENSION, outputstream);
	}

	static Color getColor(int i, int j) {
		if (i > 255) {
			i = 255;
		}
		if (j > 255) {
			j = 255;
		}
		int k = i + random.nextInt(j - i);
		int l = i + random.nextInt(j - i);
		int i1 = i + random.nextInt(j - i);
		return new Color(k, l, i1);
	}

	private static final Random random = new Random();

	private static final String EXTENSION = "JPEG";


	private static final int PLANI = 0;

	private static final int BOLD = 1;

	private static final int ITALIC = 2;

	private static final int BLOD_ITALIC = 3;

	private static final Font FONTS[] = { new Font("Courier New", 1, 16),
			new Font("Helvetica", 3, 17), new Font("Garamond", 1, 17),
			new Font("monospace", 3, 18), new Font("Monotype Corsiva", 1, 18),
			new Font("Tahoma", 3, 18), new Font("Roma", 1, 17),
			new Font("Terminal", 3, 17), new Font("Comic Sans MS", 3, 17) };

	private static final FontMetrics FONTMETRICS[] = {
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[0]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[1]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[2]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[3]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[4]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[5]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[6]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[7]),
			Toolkit.getDefaultToolkit().getFontMetrics(FONTS[8]) };

	/**
	 * 
	 * 获取一个新的随机确认码
	 * 
	 * @return 验证码
	 * 
	 */
	public static String getVerifyCode() {
		verfityCode = genRandomPasswd(verfycodeLength).toUpperCase();

		return verfityCode;
	}
	
	static String genRandomPasswd(int len){
		if (len < 1)
			return null;
		
		int n = new Double(Math.pow(10, len)).intValue();
		Random random = new Random();
		String s = new Integer(random.nextInt(n)).toString();
		
		while (s.length() < len){
			s = "0" + s;
		}
		return s;
	}
	

	/**
	 * 将验证码置null
	 */
	public static void resetVerifyCode() {
		verfityCode = null;
	}

	private static String verfityCode;

	/**
	 * TODO 验证码长度
	 */
	private static int verfycodeLength = 4;
}
